package com.sulent.frame.web.config;

import org.jetbrains.annotations.NotNull;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @ClassName SubThreadPool
 * @Description: TODO
 * @Author Submerge.
 * @Since 2025/3/19 19:03
 * @Version 1.0
 */
public class SubThreadPool<Job extends Runnable> extends AbstractExecutorService {


    private static final int DEFAULT_WORK_THREAD_COUNT = 5;
    private static final int MAX_WORK_THREAD_COUNT = 10;
    private static final int MIN_WORK_THREAD_COUNT = 1;
    private static final int workThreadCount = 5;
    private LinkedList<SubWorker> subWorkers = new LinkedList<>();
    private final LinkedList<SubJob> jobs = new LinkedList<>();
    private AtomicInteger threadNum = new AtomicInteger(0);
    private volatile boolean running = true;//线程使用时查看最新状态


    public SubThreadPool() {
        initWorkers();
    }

    private void initWorkers() {
        for (int i = 0; i < DEFAULT_WORK_THREAD_COUNT; i++) {
            SubWorker worker = new SubWorker();
            Thread thread = new Thread(worker, "sub-thread-" + threadNum.incrementAndGet());
            thread.start();
            subWorkers.addLast(worker);
        }
    }


    public void addWorks(int workThreadCount){


    }





    @Override
    public void shutdown() {
        running = false;
    }

    @NotNull
    @Override
    public List<Runnable> shutdownNow() {

        return Collections.emptyList();//立即关闭
    }

    @Override
    public boolean isShutdown() {
        return running;//优雅关闭，工作线程停止死循环，是否影响已在执行或在等待的线程？
    }

    @Override
    public boolean isTerminated() {
        return false;
    }

    @Override
    public boolean awaitTermination(long timeout, @NotNull TimeUnit unit) throws InterruptedException {
        return false;
    }

    @Override
    public void execute(@NotNull Runnable command) {
        addJobs(command);
    }

    private void addJobs(Runnable command) {
        synchronized (jobs){
            jobs.addLast((SubJob) command);//加到任务队列尾部
            jobs.notify();
            System.out.println(Thread.currentThread().getName()+"添加一个任务并唤醒一个等待工作线程...");
        }

    }

    class SubWorker implements Runnable {

        @Override
        public void run() {
            while (running) {
                SubJob job = null;
                synchronized (jobs) {
                    Thread work = Thread.currentThread();
                    if (jobs.isEmpty()) {
                        try {
                            System.out.println(work.getName() + "--工作线程进入等待队列..");
                            jobs.wait();//进入等待队列并释放锁 jobs
                            System.out.println(work.getName() + "--工作线程被唤醒..");
                        } catch (InterruptedException e) {
                            System.out.println(work.getName() + "--工作线程收到中断提醒..");
                            work.interrupt();//中断自己
                        }
                    } else {
                        System.out.println(work.getName() + "--工作线程执行..");
                        job = jobs.removeFirst();
                        job.run();
                    }

                }

            }

        }
    }

    //测试验证，创建线程池，执行十个线程任务
    public static void main(String[] args) {
        SubThreadPool<SubJob> subThreadPool = new SubThreadPool<>();
        for (int i = 0; i < 10; i++) {
            SubJob job = new SubJob();
            subThreadPool.execute(job);
            try {
                Thread.sleep(100);//模拟用户请求延时
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

        //subThreadPool.shutdown();
        //Thread.currentThread().interrupt();
    }
}


class SubJob implements Runnable{

    @Override
    public void run() {
        System.out.println("SubJob 任务执行了。。");
    }

}
